gc()

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
setwd("../../")

## Load/install packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse,
               parallel,
               ggplot2,
               data.table,
               viridis,
               miceadds,
               haven,
               cdlTools,
               rgdal,
               sf)


get_fires <- function(sample) {

  ignition_ids <- duration_data %>% filter(firenum %in% sample)
  ignition_ids <- ignition_ids$ignitionid


  df <- read_dta("data/IgnTableWest.dta") %>%
    dplyr::select(c("IgnitionID","STATE","OWNER_DESCR","FIRE_YEAR","FIRE_SIZE","DISCOVERY_DATE","STAT_CAUSE_DESCR")) %>%
    filter(IgnitionID %in% ignition_ids)

  return(df)

}

duration_data <- read_dta("data/20-by-1km_15deg/data_centroid.dta") %>%
                    group_by(firenum,ignitionid) %>%
                    summarize(num = sum(1))

sample.df <- read_dta("data/20-by-1km_15deg/sample_census.dta")
sample_census <- sample.df %>% filter(sample == 1)
sample_census <- unique(sample_census$firenum)
df_census <- get_fires(sample_census)

sample.df <- read_dta("data/20-by-1km_15deg/sample_assessors.dta")
sample_assessors <- sample.df %>% filter(sample == 1)
sample_assessors <- unique(sample_assessors$firenum)
df_assessors <- get_fires(sample_assessors)

fgdb = "data/geo/FireOccurrence_KShort/Data/FPA_FOD_20170508.gdb"
subset(ogrDrivers(), grepl("GDB", name))
fc_list = ogrListLayers(fgdb)
fires <- sf::st_read(dsn = fgdb, layer = "Fires")
df_all <- fires %>% filter(FIRE_YEAR %in% seq(1999,2015),
                          STATE %in% c("AZ","CA","CO","ID","MT","NM","NV","OR","WA","WY","UT")) %>%
            st_drop_geometry()

make_panels <- function(df) {

  df<- df %>% mutate(state.name = fips(STATE,"Name"),
                month = month(DISCOVERY_DATE),
                ownership = as.factor(case_when(OWNER_DESCR %in% c("USFS") ~ "USFS",
                                      OWNER_DESCR %in% c("BLM") ~ "BLM",
                                      OWNER_DESCR %in% c("NPS") ~ "NPS",
                                      OWNER_DESCR %in% c("BIA","TRIBAL") ~ "BIA and Tribal",
                                      OWNER_DESCR %in% c("FWS","OTHER FEDERAL", "BOR","UNDEFINED FEDERAL") ~ "Other Federal",
                                      OWNER_DESCR %in% c("STATE") ~ "State",
                                      OWNER_DESCR %in% c("COUNTY") ~ "County",
                                      OWNER_DESCR %in% c("PRIVATE") ~ "Private",
                                      OWNER_DESCR %in% c("MUNICIPAL/LOCAL") ~ "Other",
                                      OWNER_DESCR %in% c("MISSING/NOT SPECIFIED","STATE OR PRIVATE") ~ "Missing")),
                year = case_when(FIRE_YEAR %in% seq(1999,2000) ~ "1999--2000",
                                 FIRE_YEAR %in% seq(2001,2005) ~ "2001--2005",
                                 FIRE_YEAR %in% seq(2006,2010) ~ "2006--2010",
                                 FIRE_YEAR %in% seq(2011,2015) ~ "2011--2015"),
                size = case_when(FIRE_SIZE >= 0 & FIRE_SIZE < 500 ~ "0--500",
                                 FIRE_SIZE >= 500 & FIRE_SIZE < 1000 ~ "1,000--5,000",
                                 FIRE_SIZE >= 1000 & FIRE_SIZE < 5000 ~ "1,000--5,000",
                                 FIRE_SIZE >= 5000 & FIRE_SIZE < 10000 ~ "5,000--10,000",
                                 FIRE_SIZE >= 10000 & FIRE_SIZE < 25000 ~ "10,000--25,000",
                                 FIRE_SIZE >= 25000 & FIRE_SIZE < 50000 ~ "25,000--50,000",
                                 FIRE_SIZE >= 50000 & FIRE_SIZE < 100000 ~ "50,000--100,000",
                                 FIRE_SIZE >= 100000 ~ "100,000 or more"))
  df$monthname <- month.name[df$month]

  state <- df %>% group_by(state.name) %>% tally() %>%
            ungroup() %>%
            mutate(N = sum(n),
                 pct = n/N,
                 pct =  formatC(pct, format = "f", digits=2)) %>%
            select(-c("N","n"))

  ownership_levels <- c("USFS","BLM","NPS","BIA and Tribal","Other Federal","State","County","Private","Other","Missing")
  ownership <- df %>% mutate(ownership = factor(ownership, levels = ownership_levels)) %>%
                      group_by(ownership) %>% tally() %>%
                      ungroup() %>%
                      mutate(N = sum(n),
                             pct = n/N,
                             pct =  formatC(pct, format = "f", digits=2)) %>%
                      select(-c("N","n"))

  year <- df %>% group_by(year) %>% tally() %>%
              ungroup() %>%
              mutate(N = sum(n),
                     pct = n/N,
                     pct =  formatC(pct, format = "f", digits=2)) %>%
              select(-c("N","n"))

  month.df <- df %>% mutate(monthname = factor(monthname, levels = month.name)) %>% group_by(monthname) %>% tally() %>%
              ungroup() %>%
              mutate(N = sum(n),
                     pct = n/N,
                     pct =  formatC(pct, format = "f", digits=2)) %>%
              select(-c("N","n"))

  cause <- df %>% group_by(STAT_CAUSE_DESCR) %>% tally() %>% arrange(-n) %>%
              ungroup() %>%
              mutate(N = sum(n),
                     pct = n/N,
                     pct =  formatC(pct, format = "f", digits=2)) %>%
              select(-c("N","n"))

  size_levels <- c("0--500","500--1,000","1,000--5,000","5,000--10,000","10,000--25,000","25,000--50,000","50,000--100,000","100,000 or more")
  size <- df  %>% mutate(size.f = factor(size,levels = size_levels)) %>% group_by(size.f) %>% tally() %>%
              ungroup() %>%
              mutate(N = sum(n),
                     pct = n/N,
                     pct =  formatC(pct, format = "f", digits=2)) %>%
              select(-c("N","n"))


  make_panel <- function(df, title) {

    df <- as.matrix(df)

    title_row <- c(sprintf("%s",title),"","")
    return(rbind(title_row,df))

  }

  state_panel <- make_panel(state,"State")
  ownership_panel <- make_panel(ownership,"Ownership")
  year_panel <- make_panel(year,"Year")
  month_panel <- make_panel(month.df,"Month")
  cause_panel <- make_panel(cause,"Cause")
  size_panel <- make_panel(size,"Size (Acres)")

  return(list(state_panel,ownership_panel,year_panel,month_panel,cause_panel,size_panel))

}

census_panels <- make_panels(df_census)
assessors_panels <- make_panels(df_assessors)
all_panels <- make_panels(df_all)


# Format panels ----------------------------------------------------------------

state_panel <- merge(census_panels[[1]][-(1:1),], assessors_panels[[1]][-(1:1),], by = colnames(census_panels[[1]])[1],
                     all.x = TRUE,sort = FALSE) %>%
                   merge(.,all_panels[[1]][-(1:1),] %>% as.data.frame(), sort = FALSE) %>%
                  mutate(pct.y = ifelse(is.na(pct.y),0,pct.y)) %>%
                  arrange(state.name)

ownership_levels <- c("USFS","BLM","NPS","BIA and Tribal","Other Federal","State","County","Private","Other","Missing")
ownership_panel <- merge(census_panels[[2]][-(1:1),], assessors_panels[[2]][-(1:1),], by = colnames(census_panels[[2]])[1],
                         all.x = TRUE,sort = FALSE) %>%
                    merge(.,all_panels[[2]][-(1:1),] %>% as.data.frame(), sort = FALSE) %>%
                  mutate(pct.y = ifelse(is.na(pct.y),0,pct.y),
                         ownership = factor(ownership, levels = c(ownership_levels,"Total"))) %>% arrange(ownership)

year_panel <- merge(census_panels[[3]][-(1:1),],
                    data.frame(lapply(assessors_panels[[3]][-(1:1),], function(x) t(data.frame(x)))), #Convert 1 row list to dataframe
                    by.x = "year", by.y = "year",
                    all.x = TRUE, sort = FALSE)%>%
                  merge(.,all_panels[[3]][-(1:1),] %>% as.data.frame(), sort = FALSE) %>%
                  mutate(pct.y = ifelse(is.na(pct.y),0,pct.y)) %>% arrange(year)
month_panel <- merge(census_panels[[4]][-(1:1),], assessors_panels[[4]][-(1:1),], by = colnames(census_panels[[4]])[1],
                    all.x = TRUE,sort = FALSE) %>%
                  merge(.,all_panels[[4]][-(1:1),] %>% as.data.frame(), sort = FALSE) %>%
                  mutate(pct.y = ifelse(is.na(pct.y),0,pct.y),
                    monthname = factor(monthname, levels = c(month.name,"Total"))) %>% arrange(monthname)
cause_panel <- merge(census_panels[[5]][-(1:1),], assessors_panels[[5]][-(1:1),], by = colnames(census_panels[[5]])[1],
                    all.x = TRUE,sort = FALSE) %>%
                  merge(.,all_panels[[5]][-(1:1),] %>% as.data.frame(), sort = FALSE) %>%
                  mutate(pct.y = ifelse(is.na(pct.y),0,pct.y)) %>%
                    arrange(desc(pct))
size_levels <- c("0--500","500--1,000","1,000--5,000","5,000--10,000","10,000--25,000","25,000--50,000","50,000--100,000","100,000 or more")
size_panel <- merge(census_panels[[6]][-(1:1),],assessors_panels[[6]][-(1:1),], by = colnames(census_panels[[6]])[1],
                    all.x = TRUE,sort = FALSE) %>%
                  merge(.,all_panels[[6]][-(1:1),] %>% as.data.frame(), sort = FALSE, all.y = TRUE) %>%
                  mutate(pct.y = ifelse(is.na(pct.y),0,pct.y),
                         pct.x = ifelse(is.na(pct.x),0,pct.x),
                         size.f = factor(size.f,levels = c(size_levels,"Total"))) %>% arrange(size.f)

colnames(state_panel) <- c("var","pct.x","pct.y","pct.all")
colnames(ownership_panel) <- c("var","pct.x","pct.y","pct.all")
colnames(year_panel) <- c("var","pct.x","pct.y","pct.all")
colnames(month_panel) <- c("var","pct.x","pct.y","pct.all")
colnames(cause_panel) <- c("var","pct.x","pct.y","pct.all")
colnames(size_panel) <- c("var","pct.x","pct.y","pct.all")


# Assemble panels ---------------------------------------------------------------

buffer_row <- c("","","","")

col1 <- rbind(c("{\\em State}","","",""),
              state_panel,
              buffer_row,
              c("{\\em Ownership}","","",""),
              ownership_panel,
              buffer_row,
              c("{\\em Fire size}","","",""),
              size_panel)
col2 <- rbind(c("{\\em Cause}","","",""),
              cause_panel,
              buffer_row,
              c("{\\em Year}","","",""),
              year_panel,
              buffer_row,
              c("{\\em Month}","","",""),
              month_panel)

maxlength <- max(dim(col1)[1],dim(col2)[1])

append.rows <- function(col, maxlength) {

  numrows <- maxlength - dim(col)[1]

  if (length(numrows) > 0) {
    addlrows <- matrix(rep(buffer_row,times = numrows),ncol = 4)
    colnames(addlrows) <- c("var","pct.x","pct.y","pct.all")
    output <- rbind(col,addlrows)
  }
  else { output <- col  }

  return(output)

  }

col1 <- append.rows(col1,maxlength)
col2 <- append.rows(col2,maxlength)

table <- cbind(col1,col2)
table

dst <- "Results/Fire_Descriptives/"
dir.create(dst, recursive = TRUE, showWarnings = FALSE)

write.table(table, file = sprintf("%stable_contents.tex",dst),
            quote = FALSE, row.names = FALSE, col.names = FALSE,
            sep = '&', eol = '\\\\ \n')
